package com.surprise.db.dao.join;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.surprise.db.dao.SimpleDAO;
import com.surprise.db.entity.join.SelectMall001Entity;

/**
 * SelectMall001.java
 * @author xuling
 * @since 2017年2月14日
 */
@Repository
public class SelectMall001 extends SimpleDAO<SelectMall001Entity> {

	/**
	 * 获得所有的商场信息，并且获得用户是否已关注
	 * @param userid 用户ID
	 * @param limit limit信息
	 * @return 所有的商场信息
	 */
	public List<SelectMall001Entity> select(Long userid, String limit) {
		String sql = " SELECT mall.id, mall.name, mall.address, mall.logoPic, mall.projectPic, mall.prefix, " +			
					 " cityA.name as cityName, cityB.name as areaName, " +	
					 " COUNT(DISTINCT mall_userA.userid) as likeCount, " +	
					 " CASE mall_userB.userid WHEN ? THEN TRUE " +	
					 " ELSE FALSE END as likeType " +	
					 " FROM mall " +	
					 " LEFT JOIN city cityA ON mall.cityid = cityA.id " +	
					 " LEFT JOIN city cityB ON mall.districtid = cityB.id " +	
					 " LEFT JOIN mall_user mall_userA ON mall.id = mall_userA.mallid AND mall_userA.deleteflg = 0 " +	
					 " LEFT JOIN mall_user mall_userB ON mall.id = mall_userB.mallid AND mall_userB.deleteflg = 0 AND mall_userB.userid = ? " +	
					 " GROUP BY mall.id, mall.name, mall.address, mall.logoPic, mall.projectPic, " +	
					 " mall.prefix " + 
					 limit;
		return super.findBySql(sql, userid, userid);
	}
}
